//
//  CacheUserInfo.m
//  Youhuiju
//
//  Created by 余尚飞 on 16/8/18.
//  Copyright © 2016年 yutenys. All rights reserved.
//

#import "CacheUserInfo.h"

@implementation CacheUserInfo

+ (instancetype)sharedManager {
    static CacheUserInfo *shareManager = nil;
    static dispatch_once_t once;
    dispatch_once(&once,^{
        shareManager = [[self alloc] init];
        [shareManager getCacheValue];
        shareManager.isValid = NO;
        //[shareManage defaultValue];
    });
    return shareManager;
}

- (id) init {
    self = [super init];
    if(self) {
        
    }
    return self;
}

#pragma mark - default value

- (void)getCacheValue
{
    //个人基本信息
    self.gender = [[NSUserDefaults standardUserDefaults] objectForKey:@"gender"];
    self.bid = [[NSUserDefaults standardUserDefaults] objectForKey:@"bid"];
    self.name = [[NSUserDefaults standardUserDefaults] objectForKey:@"name"];
    self.telephone = [[NSUserDefaults standardUserDefaults] objectForKey:@"telephone"];
    self.avatar = [[NSUserDefaults standardUserDefaults] objectForKey:@"avatar"];
    self.city = [[NSUserDefaults standardUserDefaults] objectForKey:@"city"];
    self.province = [[NSUserDefaults standardUserDefaults] objectForKey:@"province"];
    self.myId = [[NSUserDefaults standardUserDefaults] objectForKey:@"myId"];
    
    //商品规格选取界面
    self.good_standards = [[NSUserDefaults standardUserDefaults]objectForKey:@"good_standards"];
    self.good_num = [[NSUserDefaults standardUserDefaults]objectForKey:@"good_num"];
    self.good_freight = [[NSUserDefaults standardUserDefaults]objectForKey:@"good_freight"];

    //判断登录状态
    self.isLogined = [[[NSUserDefaults standardUserDefaults] objectForKey:@"isLogined"] isEqualToString:@"YES"] ? YES : NO;
    
    
    self.isValid = [[[NSUserDefaults standardUserDefaults] objectForKey:@"isValid"] isEqualToString:@"YES"] ? YES : NO;
    
}

//- (void) defaultValue {
//
//    [self setProperty:@"gender" Value:@"1"];
//    [self setProperty:@"bid" Value:@"111111err809"];
//    [self setProperty:@"name" Value:@"用户名"];
//    [self setProperty:@"telephone" Value:@"11111111112"];
//    [self setProperty:@"avatar" Value:@""];
//    [self setProperty:@"isLogined" Value:@"NO"];
//    [self setProperty:@"isValid" Value:@"NO"];
//    [self setProperty:@"myId" Value:@"NULL"];
//}

//#pragma mark - 数据转化
//+ (YZUserModel *) getYZUserModelFromCacheUserModel:(CacheUserInfo *)cacheModel {
//    
//    YZUserModel *userModel = [[YZUserModel alloc] init];
//    userModel.userID = cacheModel.bid;
//    userModel.userName = cacheModel.name;
//    userModel.nickName = cacheModel.name;
//    userModel.gender = cacheModel.gender;
//    userModel.avatar = cacheModel.avatar;
//    userModel.telePhone = cacheModel.telephone;
//    return userModel;
//}

#pragma mark - setter cache
//个人基本信息
- (void)setGender:(NSString *)gender {
    [self setProperty:@"gender" Value:gender];
}
- (void)setBid:(NSString *)bid {
    [self setProperty:@"bid" Value:bid];
}
- (void)setName:(NSString *)name {
    [self setProperty:@"name" Value:name];
}
- (void)setTelephone:(NSString *)telephone {
    [self setProperty:@"telephone" Value:telephone];
}
- (void)setAvatar:(NSString *)avatar {
    [self setProperty:@"avatar" Value:avatar];
}
-(void)setMyId:(NSString *)myId
{
    [self setProperty:@"myId" Value:myId];
}
-(void)setCity:(NSString *)city
{
    [self setProperty:@"city" Value:city];
}
-(void)setProvince:(NSString *)province
{
    [self setProperty:@"province" Value:province];
}

//商品购买信息
-(void)setGood_standards:(NSString *)good_standards
{
    [self setProperty:@"good_standards" Value:good_standards];
}
-(void)setGood_num:(NSString *)good_num
{
    [self setProperty:@"good_num" Value:good_num];
}
-(void)setGood_freight:(NSString *)good_freight
{
    [self setProperty:@"good_freight" Value:good_freight];
}


- (void)setProperty:(NSString *)key Value:(NSString *)value {
    
    [[NSUserDefaults standardUserDefaults] setObject:value forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)setIpAddress:(NSString *)ipAddress {
    [self setProperty:@"ipaddress" Value:ipAddress];
}

- (void)setIsLogined:(BOOL)isLogined {
    if(isLogined) {
        [self setProperty:@"isLogined" Value:@"YES"];
    } else {
        [self setProperty:@"isLogined" Value:@"NO"];
    }
}

- (void)setIsValid:(BOOL)isValid
{
    if (isValid) {
        [self setProperty:@"isValid" Value:@"YES"];
    }else{
        [self setProperty:@"isValid" Value:@"NO"];
    }
}

#pragma mark - getter
//个人基本信息
- (NSString *)gender {
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"gender"];
}

- (NSString *)bid {
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"bid"];
}

- (NSString *)name {
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"name"];
}

- (NSString *)telephone {
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"telephone"];
}

- (NSString *)avatar {
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"avatar"];
}

-(NSString *)city
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"city"];
}

-(NSString *)province
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"province"];
}

-(NSString *)myId
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"myId"];
}
- (NSString *)ipAddress {
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"ipaddress"];
}
//商品购买信息
-(NSString *)good_standards
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"good_standards"];
}
-(NSString *)good_num
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"good_num"];
}
-(NSString *)good_freight
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"good_freight"];
}

//登录状态
- (BOOL) isLogined {
    NSString *isLogined = [[NSUserDefaults standardUserDefaults] objectForKey:@"isLogined"];
    if(isLogined == nil || isLogined.length == 0) {
        return NO;
    }
    return [isLogined isEqualToString:@"YES"];
}

- (BOOL)isValid
{
    NSString *isValid = [[NSUserDefaults standardUserDefaults] objectForKey:@"isValid"];
    if(isValid == nil || isValid.length == 0) {
        return NO;
    }
    return [isValid isEqualToString:@"YES"];
}

@end

